<template>
  <vmForm :formData="formData" @confirm="confirm"></vmForm>
</template>

<script>
import vmForm from '../component/form.vue'
export default {
  components: {
    vmForm
  },
  data() {
    return {
      formData: {
        permission_name: '',
        type: '3',
        sidebar_name: '',
        icon: '',
        route_name: '',
        meta_title: '',
        route_path: '',
        component_path: '',
        pid: [],
        api_path: '',
        order: '',
        view_name: '', // 此字段原权限管理系统为命名视图，现作为按钮名称使用
        redirect: '',
        hl_routes: '',
        /*  */
        isMainMenu: ''
      },
    }
  },
  activated() {
    if (this.$route.query?.action != 'refresh' || this.$isFirstInPage.get()) {
      this.formData = this.$options.data().formData
    }
  },
  mounted() { },
  computed: {},
  methods: {
    async submitData() {
      await this.$refs.elForm.validate()
      await this.$refs?.elFormMenu?.validate()
      await this.$refs?.elFormBtn?.validate()
      let url = ''
      let data = JSON.parse(JSON.stringify(this.formData))
      data.api_path = data.api_path.split('\n').toString()
      if (data.isMainMenu - 0 === 1) {
        data.pid = 0
      }
      if (this.$valueType(data.pid) === 'array') {
        data.pid = data.pid?.pop()
      }

    },
    async confirm(data) {
      try {
        let res = await this.request({
          url: '/admin/permission/add',
          method: 'post',
          data: {
            ...data,
            change_time: undefined,
            create_time: undefined,
            path_name: undefined
          }
        })
        this.$message.success(res.msg)
        setTimeout(() => {
          this.pageTurnParams('AuthorityList', {
            action: 'refresh'
          })
        }, 1000)
      } catch (err) {
        this.errorHandler(err)
      }
    },

  },
  watch: {
    // '$route.fullPath': function () {
    //   let name = this.$route.name
    //   if (name === 'addAuthority' || name === 'reviseAuthority') {
    //     this.formData = this.$options.data().formData
    //     this.getAuthorityTree()
    //    this.setPageTabs()
    //   }
    // }
  }
}
</script>

<style lang="scss" scoped>
@import "./style.scss";
</style>
